/*
 * @Author: your name
 * @Date: 2019-10-31 02:41:18
 * @LastEditTime: 2019-11-05 16:36:00
 * @LastEditors: Please set LastEditors
 * @Description: In User Settings Edit
 * @FilePath: /vant_new/src/utils/async-loader.js
 */
// 使用这个会导致组件内部的 router 导航守卫无法使用, 慎用
/**
 * @param  { string } chunkPath views 文件夹下的页面路径
 * @return { function } 返回 promise<component> 的匿名函数
 */
import spinner from '../components/spinner';
import Error from '../pages/other/Error'

export default chunkPath => {
  const AsyncHandler = () => ({
    component: new Promise(resolve => {
      setTimeout(() => {
        resolve(
          import(/* webpackChunkName: "[request]" */ `@/pages/${chunkPath}`)
        );
      }, 300);
    }),
    loading: spinner,
    error: {
      render(h) {
        return Error;
      }
    },
    timeout: 10000
  });
  return () =>
    Promise.resolve({
      functional: true,
      render(h, { data, children }) {
        return h(AsyncHandler, data, children);
      }
    });
};
